package FlyWeight;

import java.util.ArrayList;
import java.util.UUID;

/**
 * 子弹为例子
 */
class Bullet{
    public UUID uuid = UUID.randomUUID();
    public boolean isLive = false;

    @Override
    public String toString() {
        return "Bullet{" +
                "uuid=" + uuid +
                ", isLive=" + isLive +
                '}';
    }
}

class BulletPool{
    // pool
    private ArrayList<Bullet> list = new ArrayList<>();
    {
        for(int i = 0; i < 100; i++){
            System.out.println("creating...");
            list.add(new Bullet());
        }
    }

    public Bullet getBullet(){
        for(int i = 0; i < list.size(); i++){
            System.out.println("test");
            if(!list.get(i).isLive) {
                return list.get(i);
            }
        }

        Bullet b = new Bullet();
        list.add(b);
        return b;
    }
}

public class FlyWeight {
    public static void main(String[] args){
        System.out.println("享元设计模式");
        BulletPool bullet = new BulletPool();
        System.out.println(bullet.getBullet());
    }
}
